package com.x.erp.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.x.erp.dao.FeedbackDao;
import com.x.erp.model.Feedback;
import com.x.erp.rpc.FeedbackRpcService;
import com.x.framework.exception.BusinessException;
import com.x.framework.exception.ExceptionCode;
import com.x.framework.service.BaseService;

/**
 * 用户反馈记录服务
 */
@Component
@Service(interfaceName = "com.x.erp.rpc.FeedbackRpcService", timeout = 60000)
public class FeedbackService extends BaseService implements FeedbackRpcService {

	@Autowired
	private FeedbackDao feedbackDao;// 车辆收藏DAO
	
	@Autowired
	private FastFileStorageClient fdfsClient;//文件服务 

	@Override
	public List<Feedback> selectFeedbackList(Feedback feedback) {
		try {
			return feedbackDao.selectFeedbackList(feedback);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	@Override
	public Feedback insertFeedback(Feedback feedback) {
		Date date = new Date();
		feedback.setFeedbackCreateTime(date);
		try {
			return this.insert(feedback);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	@Override
	public int deleteFeedback(Feedback feedback){
		try {
			List<Feedback> selectFeedbackList = feedbackDao.selectFeedbackList(feedback);
			if(selectFeedbackList !=null && selectFeedbackList.size() > 0){
				for (Feedback feedback2 : selectFeedbackList) {
					String delFileId = feedback2.getFeedbackImage();
					//如果有图片。先删除图片
					if(StringUtils.isNotEmpty(delFileId)){
						fdfsClient.deleteFile(delFileId);
					}
				}
			}
		} catch (Exception e) {
//			e.printStackTrace();
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
		return this.delete(feedback);
	}

	@Override
	public int selectFeedbackNum(Feedback feedback) {
		try {
			return feedbackDao.selectFeedbackNum(feedback);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

}
